package module.map.operations.nodes
{
	import drongo.model.propertys.ArrayProperty;
	import drongo.model.values.CollectionValue;
	
	import common.operations.BaseOperation;
	
	import module.map.model.Propertys;
	
	public class DeleteNodeOperation extends BaseOperation
	{
		private var oldState:uint;
		private var deleteList:Array;
		private var mapNodes:ArrayProperty;
		public function DeleteNodeOperation()
		{
			super();
		}
		
		override public function execute():void
		{
			this.mapNodes=this.project.currentMap.Get(Propertys.NODES) as ArrayProperty;
			deleteList=[];
			var list:Array=this.project.currentMap.selector.selectedList;
			var index:int;
			for each (var i:CollectionValue in list) 
			{
				index=mapNodes.GetChildIndex(i);
				if(index>=0){
					deleteList.push({
						index:index,
						data:i
					})	
				}
			}
			oldState=this.project.currentMap.Get(Propertys.STATE).GetValue();
			redo();
			this.project.currentMap.recorder.link(this);
		}
		
		override public function redo():void
		{
			deleteList.sortOn("index",Array.NUMERIC|Array.DESCENDING);
			for each (var i:Object in deleteList) 
			{
				this.mapNodes.RemoveAt(i.index);
			}
			this.project.currentMap.Update(Propertys.STATE,1);
		}
		
		override public function undo():void
		{
			deleteList.sortOn("index",Array.NUMERIC);
			for each (var i:Object in deleteList) 
			{
				this.mapNodes.AddAt(i.index,i.data);
			}
			this.project.currentMap.Update(Propertys.STATE,this.oldState);
		}
	}
}